using System;
using UnityEngine;

public class SourceOfBlood : EventComponentArea<SourceOfBlood>
{
	public static int current = 0;

	public static BloodBase[] listDrops = new BloodBase[0];

	public string nameSource;

	public BloodBase drops;

	public Vector2 localPosition;

	[Tooltip("Локальный угол полета капель крови")]
	[Range(0f, 360f)]
	public int angle;

	private float angleGlobal;

	[Range(0f, 1f)]
	public float scaleAngleObject = 1f;

	[Tooltip("Существо, координаты которой будут использованы как координаты земли")]
	public Transform entity;

	[Tooltip("Отклонение от указаного направления")]
	[Range(0f, 180f)]
	public int deviationAngle = 10;

	public int elementsInSeconds = 10;

	public int totalElements = 15;

	public int totalMax;

	public float speedMin;

	public float speedMax = 0.1f;

	public bool spreadSpeed;

	[Tooltip("Масштаб частиц")]
	public float scale = 1f;

	[Tooltip("Пульсация. Время, в течении которого будут идти брызги, затем в течении этого же времени будет длиться приостановка")]
	public float timePulse;

	private float currentTimePulse;

	private BloodBase blood;

	private float timer;

	private float speed = 1f;

	private float stepSpeed;

	private Vector3 movePartBody = default(Vector2);

	private Rigidbody2D body;

	private Vector2 globalPosition = default(Vector2);

	public static BloodBase GetDropsList(BloodBase dropsSource)
	{
		listDrops = dropsSource.GetListObjects();
		if (listDrops.Length != 0)
		{
			current++;
			current %= listDrops.Length;
			if (listDrops[current] == null)
			{
				listDrops[current] = UnityEngine.Object.Instantiate(dropsSource.gameObject).GetComponent<BloodBase>();
				listDrops[current].name = dropsSource.name + "." + current.ToString();
			}
			else if (listDrops[current].gameObject.activeInHierarchy)
			{
				for (int i = 0; i < listDrops.Length; i++)
				{
					if (listDrops[i] != null && !listDrops[i].gameObject.activeInHierarchy)
					{
						current = i;
						break;
					}
				}
			}
			listDrops[current].OnGetDrops();
			return listDrops[current];
		}
		return null;
	}

	public override void OnDrawGizmosSelected()
	{
		base.OnDrawGizmosSelected();
		if (base.enabled)
		{
			globalPosition = base.gameObject.transform.TransformPoint(localPosition);
			Gizmos.color = new Color(1f, 0.05f, 0f);
			if (body == null)
			{
				body = base.gameObject.GetComponent<Rigidbody2D>();
			}
			if (body == null)
			{
				body = base.gameObject.GetComponentInParent<Rigidbody2D>();
			}
			angleGlobal = (float)angle * ((float)Math.PI / 180f);
			float num = angleGlobal;
			Vector3 eulerAngles = base.gameObject.transform.rotation.eulerAngles;
			angleGlobal = num + eulerAngles.z * ((float)Math.PI / 180f);
			Gizmos.DrawLine(globalPosition, new Vector2(globalPosition.x + Mathf.Cos(angleGlobal) * speedMax * 0.04f, globalPosition.y + Mathf.Sin(angleGlobal) * speedMax * 0.04f));
			angleGlobal -= (float)deviationAngle * ((float)Math.PI / 180f);
			Gizmos.DrawLine(globalPosition, new Vector2(globalPosition.x + Mathf.Cos(angleGlobal) * speedMax * 0.04f, globalPosition.y + Mathf.Sin(angleGlobal) * speedMax * 0.04f));
			angleGlobal += (float)(deviationAngle * 2) * ((float)Math.PI / 180f);
			Gizmos.DrawLine(globalPosition, new Vector2(globalPosition.x + Mathf.Cos(angleGlobal) * speedMax * 0.04f, globalPosition.y + Mathf.Sin(angleGlobal) * speedMax * 0.04f));
		}
	}

	public override bool ShowIconGizmos(AreaDamage area)
	{
		OnDrawGizmosSelected();
		return true;
	}

	private void Awake()
	{
		base.enabled = false;
	}

	public override void OnStartArea(AreaDamage area)
	{
		if (drops.GetListObjects().Length == 0)
		{
			currentTimePulse = 0f - timePulse;
			timer = -1f;
			UnityEngine.Object.Destroy(this);
		}
		else
		{
			body = base.gameObject.GetComponent<Rigidbody2D>();
			currentTimePulse = timePulse;
			base.enabled = false;
		}
	}

	public override void Activation(AreaDamage area)
	{
		Enable();
		if (onActivationListener != null)
		{
			onActivationListener(this);
		}
	}

	public void Enable()
	{
		if (drops.GetListObjects().Length != 0)
		{
			base.enabled = true;
			movePartBody = base.transform.position;
			totalElements = UnityEngine.Random.Range(totalElements, Mathf.Max(totalElements, totalMax));
			stepSpeed = (speedMax - speedMin) / (float)totalElements;
			stepSpeed = Mathf.Floor(stepSpeed * 1000f) / 1000f;
		}
	}

	public void Enable(string name)
	{
		if (drops.GetListObjects().Length == 0)
		{
			return;
		}
		SourceOfBlood[] components = base.gameObject.GetComponents<SourceOfBlood>();
		for (int i = 0; i < components.Length; i++)
		{
			if (components[i].nameSource == name)
			{
				components[i].Enable();
			}
		}
		components = null;
	}

	public void Disable(string name)
	{
		SourceOfBlood[] components = base.gameObject.GetComponents<SourceOfBlood>();
		for (int i = 0; i < components.Length; i++)
		{
			if (components[i].nameSource == name)
			{
				components[i].enabled = false;
			}
		}
		components = null;
	}

	private void Update()
	{
		if (currentTimePulse > 0f)
		{
			currentTimePulse -= Time.deltaTime;
			if (currentTimePulse <= 0f)
			{
				currentTimePulse = 0f - timePulse;
			}
		}
		else if (currentTimePulse < 0f)
		{
			currentTimePulse += Time.deltaTime;
			if (currentTimePulse >= 0f)
			{
				currentTimePulse = timePulse;
			}
		}
		if (currentTimePulse >= 0f)
		{
			timer += Time.deltaTime;
		}
		movePartBody = base.gameObject.transform.position - movePartBody;
		if (timer > 1f / (float)elementsInSeconds)
		{
			globalPosition = base.gameObject.transform.TransformPoint(localPosition);
			float num = angle;
			Vector3 eulerAngles = base.gameObject.transform.rotation.eulerAngles;
			angleGlobal = num + eulerAngles.z * scaleAngleObject;
			angleGlobal %= 360f;
			if (!(Mathf.Abs(90f - angleGlobal) < 120f))
			{
				Vector3 position = entity.position;
				if (!(Mathf.Abs(position.y - globalPosition.y) < 1f))
				{
					goto IL_0291;
				}
			}
			speed = speedMin + UnityEngine.Random.value * (speedMax - speedMin);
			if (spreadSpeed)
			{
				speed = speedMin + (float)totalElements * stepSpeed;
			}
			blood = GetDropsList(drops);
			if (blood != null)
			{
				blood.gameObject.transform.position = globalPosition;
				blood.SetMove(speed, angleGlobal + (float)UnityEngine.Random.Range(-deviationAngle, deviationAngle));
				BloodBase bloodBase = blood;
				Vector3 position2 = entity.position;
				bloodBase.SetFloor(position2.y);
				blood.AddSpeedBody(movePartBody);
				blood.SetScale(scale, scale);
			}
			goto IL_0291;
		}
		goto IL_02aa;
		IL_02aa:
		movePartBody = base.gameObject.transform.position;
		if (totalElements <= 0)
		{
			UnityEngine.Object.Destroy(this);
		}
		return;
		IL_0291:
		timer = 0f;
		totalElements--;
		goto IL_02aa;
	}
}
